package com.lz.fruitday;

import android.app.Application;
import android.widget.Toast;

import com.lidroid.xutils.DbUtils;
import com.lz.fruitday.netstate.NetChangeObserver;
import com.lz.fruitday.netstate.NetWorkUtil;
import com.lz.fruitday.netstate.NetworkStateReceiver;
import com.lz.fruitday.utils.AppConfig;
import com.lz.fruitday.utils.FileUtils;
import com.lz.fruitday.utils.ImageLoaderUtil;
import com.lz.fruitday.utils.XutilsHelper;

import cn.jpush.android.api.JPushInterface;
import cn.sharesdk.framework.ShareSDK;

/**
 * Created by liuZhi
 * 2015/10/19
 *
 * 推送AppKey 		abdc57343365d9ab3fcb14b6
 */
public class BaseApp extends Application {

    public static BaseApp app;

    private NetChangeObserver observer=new NetChangeObserver(){
        @Override
        public void onConnect(NetWorkUtil.NetType type) {
            super.onConnect(type);
            switch (type) {
                case WIFI:
                   // showToast("WIFI已连接");
                    break;
                case GNET_3:
                  //  showToast("移动3G网络已连接");
                    break;
                default:
                 //   showToast("网络已连接");
                    break;
            }
        }

        @Override
        public void onDisConnect() {
            super.onDisConnect();
            showToast("网络不可用,请重新设置");
        }
    };

    public static BaseApp getInstance(){
        return app;
    }
    @Override
    public void onCreate() {
        super.onCreate();
        app=this;
        //初始化推送SDK
        JPushInterface.init(this);
        //初始化ShareSDK
        ShareSDK.initSDK(getApplicationContext());
        //初始化网络状态判断
        initNetState();
        //图片加载框架
        ImageLoaderUtil.configGolbalImageLoader(this);

        XutilsHelper.getInstance().GonfigGlobalBitmap(this);

    }

    private void initNetState() {
        NetworkStateReceiver.registerNetworkStateReceiver(this);
        NetworkStateReceiver.registerObserver(observer);
    }

    public void unegisterNetworkStateReceiver() {
        NetworkStateReceiver.removeRegisterObserver(observer);
        NetworkStateReceiver.unRegisterNetworkStateReceiver(this);
    }

    public void showToast(String msg) {
        Toast.makeText(BaseApp.this, msg, Toast.LENGTH_SHORT).show();
    }


    public DbUtils getDbUtils() {
        DbUtils.DaoConfig config = new DbUtils.DaoConfig(this);
        config.setDbName(AppConfig.DB_NAME);
        config.setDbVersion(AppConfig.DB_VERSION);
        config.setDbDir(FileUtils.getDBPath(this));
        config.setDbUpgradeListener(new DbUtils.DbUpgradeListener() {
            @Override
            public void onUpgrade(DbUtils db, int oldVersion, int newVersion) {

            }
        });
        return DbUtils.create(config);
    }
}
